﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CustomsSystem.DAL;
using CustomsSystem.Model;
using System.Data;

namespace CustomsSystem.BLL
{
    public class CustomerManage
    {
        public CustomerService dal = new CustomerService();


        /*public DataTable GetCustomerInfo()
        {
            DataSet ds = CustomerService.GetCustomerDataSet();
            if (ds != null)
                return ds.Tables[0];
            else return null;
        }
         
        public DataTable GetCustomerInfo(string id)
        {
            DataSet ds = CustomerService.GetCustomerDataSet(id);
            if (ds != null)
                return ds.Tables[0];
            else return null;
        }
         */
        /// <summary>
        /// 获得数据DATASET列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获取gv视图
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetViewList(string strWhere)
        {
            return dal.GetViewList(strWhere);
        }

        /// <summary>
        /// 获得数据实体列表
        /// </summary>
        public List<CustomsSystem.Model.Customer> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
            //List<CustomsSystem.Model.Customer> modelList = new List<CustomsSystem.Model.Customer>();
            //CustomsSystem.Model.Customer model;
            //model=dal.GetModel(strWhere);
            //if(model!=null)
            //{
            //    modelList.Add(model);
            //}
            //return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<CustomsSystem.Model.Customer> DataTableToList(DataTable dt)
        {
            List<CustomsSystem.Model.Customer> modelList = new List<CustomsSystem.Model.Customer>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CustomsSystem.Model.Customer model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Customer GetModel(int ID)
        {
            return dal.GetModel(ID);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public CustomsSystem.Model.Customer GetModel(DataTable dt)
        {
            return dal.DataRowToModel(dt.Rows[0]);
        }
        /// <summary>
        /// 根据公司名称查找
        /// </summary>
        /// <param name="strwhere"></param>
        /// <returns></returns>
        public CustomsSystem.Model.Customer GetModel(string strwhere)
        {
            return dal.GetModel(strwhere);
        }
        /*
        public Customer GetCusFromTable(DataTable dt)
        {
            if (dt == null) return null;
                Customer myCus = new Customer();
                myCus.Id = Convert.ToInt32( dt.Rows[0]["ID"]);
                myCus.CustomerName = dt.Rows[0]["CustomerName"].ToString();
                myCus.Address = dt.Rows[0]["Address"].ToString();
                myCus.CustomerPhone = dt.Rows[0]["CustomerPhone"].ToString();
                myCus.CustomerType = dt.Rows[0]["CustomerType"].ToString();
                myCus.Fax = dt.Rows[0]["Fax"].ToString();
                myCus.Invoice = dt.Rows[0]["Invoice"].ToString();
                myCus.PostCode = dt.Rows[0]["PostCode"].ToString();
                myCus.Remarks = dt.Rows[0]["Remarks"].ToString();
                return myCus;
        }
        */



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CustomsSystem.Model.Customer model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Customer model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            return dal.Delete(ID);
        }
    }
}
